<?php

namespace app\activity\controller;

use library\Controller;
use think\facade\Request;


/** 活动
 * Created by PhpStorm.
 * User: ycp
 * Date: 2019/11/18
 * Time: 21:07
 */

class Index extends Controller
{
    protected $table = 'lite_activity';
    protected $groupTable = 'lite_activity_group';
    protected $cateTable = 'lite_activity_cate';

    //活动首页
    public function index()
    {
        $this->title = '活动管理';
        $where = ['is_deleted' => '0'];
        $this->statusTitle = ['0' => '未开始', '1' => '进行中', '2' => '已结束'];
        $this->_query($this->table)->like('name')->where($where)->order('sort', 'desc')->order('id', 'desc')->page();
    }

    //添加活动
    public function add()
    {
        $this->title = '添加活动';
        $this->isAddMode = '1';
        $this->typeData = config('data.activity_type');
        $this->applyCsrfToken();
        return $this->_form($this->table, 'form');
    }

    public function edit()
    {
        $this->title = '编辑活动';
        $this->typeData = config('data.activity_type');
        $this->isAddMode = '0';
        $this->applyCsrfToken();
        return $this->_form($this->table, 'form');
    }

    /**
     * 表单结果处理
     * @param boolean $result
     */
    protected function _form_result($result)
    {
        if ($result && $this->request->isPost()) {
            $data = $this->request->post();
            if($this->isAddMode == 1){
                //添加默认组别
                db($this->groupTable)->insert([
                    'name' => '默认组别',
                    'price' => '0',
                    'list_price' => '0',
                    'status' => '1',
                    'create_time' => date('Y-m-d H:i:s'),
                    'count' => -1,
                    'activity_id' => $result,
                ]);
                //添加分组
                foreach ($data['type'] as $val){
                    db($this->cateTable)->insert([
                        'cate_id' => $val,
                        'activity_id' => $result,
                    ]);
                }
            }else{
                db($this->cateTable)->where([
                    'activity_id' => $result,
                ])->delete();
                //添加分组
                foreach ($data['type'] as $val){
                    db($this->cateTable)->insert([
                        'cate_id' => $val,
                        'activity_id' => $result,
                    ]);
                }
            }
            $this->success('活动编辑成功！', 'javascript:history.back()');
        }
    }

    public function del()
    {
        $this->applyCsrfToken();
        $this->_delete($this->table);
    }


    public function _form_filter(&$data)
    {
        if ($this->request->isGet()) {
            if($data){
                $data['apply_end_time'] = substr($data['apply_end_time'], 0, 10);
                $data['apply_start_time'] = substr($data['apply_start_time'], 0, 10);
                $data['end_time'] = substr($data['end_time'], 0, 10);
                $data['start_time'] = substr($data['start_time'], 0, 10);
               if(isset($data['id'])){
                   $type = db($this->cateTable)->where([
                       'activity_id' => $data['id'],
                   ])->column('id,cate_id', 'id');
                   $data['type'] = $type;
               }

            }
        } elseif ($this->request->isPost()) {
            if(empty($data['type'])){
                $this->error('请选择活动类型！');
            }
            //判断当前status 条件
            $now = time();
            $startTime = strtotime($data['apply_start_time']);
            $endTime = strtotime($data['apply_end_time']);
            if($now > $startTime && $now< $endTime){
                $data['status'] = 1;
            }
            if($now < $startTime){
                $data['status'] = 0;
            }
            if($now > $endTime){
                $data['status'] = 2;
            }
            $data['create_time'] = date('Y-m-d H:i:s');
        }
    }

    public function showIndex()
    {
        $this->_save($this->table, ['is_show_index' => $this->request->param('status')]);
    }
    
    
    //关联服务
    public function addProduct()
    {
        $activityId = $this->request->param('activity_id');
        if(Request::instance()->isGet()){
            $product = db('store_goods')->where(['is_deleted' => 0])->column('id, title', 'id');
            //已有數據
            $hasProduct = db('lite_activity_product')->where(['activity_id' => $activityId])->column('id, product_id', 'id');
            $this->assign('hasProduct', $hasProduct);
            $this->assign('product', $product);
            return $this->fetch();
        }else{
            $productId = $this->request->param('productId');
            $time = date('Y-m-d H:i:s');
            //刪除
            db('lite_activity_product')->where(['activity_id' => $activityId])->delete();
            foreach ($productId as $key => $value){
                $res = db('lite_activity_product')->insert([
                   'product_id' => $value,
                   'activity_id' => $activityId,
                   'create_time' => $time,
                ]);
            }
            $this->success('数据记录保存成功!', '');
        }
    }

}